<?php
namespace App\Services\Manage;

use App\Models\Manage\ApiCate as ApiCateModel;
use Illuminate\Support\Facades\DB;

class ApiCate
{

    public function getCateTree($table = '')
    {

        $table = $table ? 'api_cate' . '_' . $table : 'api_cate';
        $list = DB::table($table)->where('status', '1')
        ->orderBy('level', 'ASC')->orderBy('parent_id', 'ASC')->orderBy('order', 'ASC')->get()->toArray();
//        $list = ApiCateModel::where('status', '1')
//            ->orderBy('level', 'ASC')->orderBy('parent_id', 'ASC')->orderBy('order', 'ASC')->get()->toArray();
        $tree = [];
        if( is_array($list) ){
            foreach ($list as $k => $v) {
                if (is_object($v))
                    $v = (array) $v;
                if( 1 == $v['level'] ){
                    $tree[$v['id']] = $v;
                    unset( $list[$k] );
                }
            }
            foreach ($list as $k => $v) {
                if (is_object($v))
                    $v = (array) $v;
                if( 2 == $v['level']){
                    $tree[$v['parent_id']]['children'][$v['id']] = $v;
                    unset( $list[$k] );
                }
            }
        }

        return $tree;
    }
//    // 增加一条分类记录
//    public function addCate ($data)
//    {
//        return $this->model('app\manage\model\ApiCate')->save($data);
//    }
//    // 禁用一条接口分类
//    public function removeCate ($data)
//    {
//        $apiCate['id'] = $data['id'];
//        $apiCate['status'] = 0;
//        return $this->model('app\manage\model\ApiCate')::update($apiCate);
//    }



}